package com.huchao.www.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.huchao.www.Listener.StudentDataListener;
import com.huchao.www.dao.StudentMapper;
import com.huchao.www.exception.MyException;
import com.huchao.www.pojo.StudentData;
import com.huchao.www.pojo.StudentInfo;
import com.huchao.www.service.api.StudentService;
import com.huchao.www.pojo.StudentPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @Description:
 * @BelongsProject: background-management-v10
 * @BelongsPackage: com.huchao.www.service.impl
 * @Author: 亖思
 * @CreateTime: 2023/3/10 18:11
 * @Version: 1.0
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;

    /**
     * 〈添加学生〉
     *
     * @param studentInfo:
     * @Author: 亖思
     * @Date: 2023/3/11 11:07
     * @return: void
     **/
    @Override
    public void addStudent(StudentInfo studentInfo) {
        studentMapper.addStudent(studentInfo);
    }

    /**
     * 〈分页查询〉
     *
     * @param map      :开始日、截止日、用户名的map
     * @param pageNum  :页数
     * @param pageSize :页面容量
     * @Author: 亖思
     * @Date: 2023/3/11 11:07
     * @return: java.util.List<com.huchao.www.pojo.StudentInfo>
     **/
    @Override
    public List<StudentPo> findStuPageList(HashMap<String, Object> map, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize, true);
        return studentMapper.findAllPageList(map);
    }

    /**
     * 〈多表联查学生数据〉
     *
     * @Author: 亖思
     * @Date: 2023/3/17 11:15
     * @return: java.util.List<com.huchao.www.pojo.StudentData>
     **/
    @Override
    public List<StudentData> findAllStu() {
        return studentMapper.findAllStu();
    }

    /**
     * 〈根据StudentDataList添加学生信息〉
     *
     * @param studentDataList:
     * @Author: 亖思
     * @Date: 2023/3/15 21:32
     * @return: void
     **/
    @Override
    public void addStuByStudentDataList(List<StudentData> studentDataList) {
        studentMapper.addStuByStudentDataList(studentDataList);
    }

    /**
     * 〈导入Excel文件〉
     *
     * @param file :
     * @Author: 亖思
     * @Date: 2023/3/15 13:19
     * @return: java.lang.String
     **/
    @Override
    public String importData(MultipartFile file) throws MyException, IOException {
        EasyExcel.read(file.getInputStream(), StudentData.class, new StudentDataListener(studentMapper))
                .sheet()
                .doRead();
        String msg = StudentDataListener.getMsg();
        if (!"success".equals(msg)) {
            throw new MyException(msg);
        }
        return msg;
    }
}

